package com.bignerdranch.android.expresshelper.Fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bignerdranch.android.expresshelper.Activity.ResultActivity;
import com.bignerdranch.android.expresshelper.Activity.WarningDialog;
import com.bignerdranch.android.expresshelper.Adapter.HistoryAdapter;
import com.bignerdranch.android.expresshelper.Bean.ExpressBean;
import com.bignerdranch.android.expresshelper.Dao.ExpressDao;
import com.bignerdranch.android.expresshelper.R;
import com.swipelistview.BaseSwipeListViewListener;
import com.swipelistview.SwipeListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/24.
 */
public class HistoryFragment extends Fragment {
    private TextView mTopText;
    private SwipeListView mListView;
    private HistoryAdapter mAdapter;
    private ScrollView mScrollView;
    private LinearLayout mLinearLayout;
    private ImageButton mRightImage;
    private List<ExpressBean> mDate;
    ExpressDao mDao;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_history,container,false);
        mTopText = (TextView)view.findViewById(R.id.top_text);
        mTopText.setText(R.string.history);

        mRightImage = (ImageButton) view.findViewById(R.id.top_img_right) ;
        mRightImage.setImageResource(R.drawable.delete);
        mDate= new ArrayList<>();

        mRightImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Dialog dialog1 = new AlertDialog.Builder(getContext()).setTitle(R.string.warning).setMessage(R.string.message)

                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mDate.clear();
                        mDao.deleteAll();
                        mAdapter.notifyDataSetChanged();
                        mScrollView.setVisibility(View.INVISIBLE);
                        mLinearLayout.setVisibility(View.VISIBLE);
                        mRightImage.setEnabled(false);
//                        Toast.makeText(getActivity(),"当前无记录，清空按钮不可用",Toast.LENGTH_SHORT).show();

                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })

                        .create();
                dialog1.show();


            }
        });




        mScrollView = (ScrollView) view.findViewById(R.id.history_list);
        mLinearLayout = (LinearLayout)view.findViewById(R.id.nothing);

        mListView = (SwipeListView)view.findViewById(R.id.datalist);
        mListView.setSwipeMode(SwipeListView.SWIPE_MODE_LEFT);
        int deviceWidth =  getResources().getDisplayMetrics().widthPixels;
        mListView.setOffsetLeft(deviceWidth * 0.8f);
        mListView.setOffsetRight(deviceWidth *0.8f);


        updateUI(view);
        mListView.setSwipeListViewListener(new BaseSwipeListViewListener(){
            @Override
            public void onClickFrontView(int position) {
                super.onClickFrontView(position);

                Intent i = new Intent(getActivity(), ResultActivity.class);
                i.putExtra("num",mAdapter.getNum(position));
                i.putExtra("name",mAdapter.getSimName(position) );
                startActivity(i);

            }
        });

        return view;
    }

    private void updateUI(View view) {
        mDao = new ExpressDao(getContext());
        mDate = mDao.queryAll();
        mAdapter = new HistoryAdapter(getActivity().getApplicationContext(),view,mDate);
        mListView.setAdapter(mAdapter);

        if (mListView.getCount()>0){
            mScrollView.setVisibility(View.VISIBLE);
            mLinearLayout.setVisibility(View.INVISIBLE);
        }else {
            mScrollView.setVisibility(View.INVISIBLE);
            mLinearLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mDate.isEmpty()){
            mRightImage.setEnabled(false);
//            Toast.makeText(getActivity(),"当前无记录，清空按钮不可用",Toast.LENGTH_SHORT).show();
        }else {
            mRightImage.setEnabled(true);
        }
    }
}
